package com.simpletour.qa.service.controller;

import com.simpletour.qa.common.http.ObjectRestResponse;
import com.simpletour.qa.common.http.ResponseCode;
import com.simpletour.qa.common.http.ResponseMsg;
import com.simpletour.qa.service.entity.Solution;
import com.simpletour.qa.service.service.SolutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RequestMapping(value = "/solution")
@RestController
public class SolutionController {

    @Autowired
    private SolutionService solutionService;


    @RequestMapping(value = "",method = RequestMethod.POST)
    public ObjectRestResponse add(@RequestBody @Valid Solution solution, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            List<FieldError> errorList = bindingResult.getFieldErrors();
            return ObjectRestResponse.builder().code(ResponseCode.PARAM_CHECK_ERROR).error("参数不能为空").build();
        }
        //todo 新增失败校验
        solutionService.save(solution);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).msg(ResponseMsg.SUCCESS).build();
    }


    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ObjectRestResponse get(@PathVariable Integer id){
        Solution object= (Solution) solutionService.findById(id).orElse(null);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).result(object).msg(ResponseMsg.SUCCESS).build();
    }

   /* @ApiOperation(value = "通过id更新")
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public ObjectRestResponse update(@PathVariable Integer id, @RequestBody Solution solution){
        try {
            ReflectionUtils.setPrimaryKey(solution,id);
        } catch (Exception e) {
            return ObjectRestResponse.builder().code(ResponseCode.SERVER_INTERNAL_ERROR);
        }
        solutionService.save(solution);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).msg(ResponseMsg.SUCCESS);
    }*/

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ObjectRestResponse remove(@PathVariable Integer id){
        solutionService.deleteById(id);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).msg(ResponseMsg.SUCCESS).build();
    }


    @RequestMapping(value = "/all",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public ObjectRestResponse all(@RequestParam(defaultValue = "0",name = "page") Integer page,
                                  @RequestParam(defaultValue = "15",name = "fetch") Integer fetch,
                                  @RequestParam(name = "questionId",required = false) Integer questionId){
        List<Solution> all = solutionService.findAll(page,fetch,questionId);
        return ObjectRestResponse.builder().code(ResponseCode.SUCCESS).result(all).msg(ResponseMsg.SUCCESS).build();
    }
}
